package com.thinkverse.paper.controller;

import com.thinkverse.common.result.Result;
import com.thinkverse.paper.entity.Paper;
import com.thinkverse.paper.service.PaperService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import jakarta.validation.Valid;

/**
 * 论文控制器
 */
@RestController
@RequestMapping("/paper")
@RequiredArgsConstructor
public class PaperController {
    
    private final PaperService paperService;
    
    /**
     * 上传论文
     */
    @PostMapping("/upload")
    public Result<String> uploadPaper(@RequestParam("file") MultipartFile file,
                                    @RequestParam("title") String title,
                                    @RequestParam("abstractText") String abstractText,
                                    @RequestParam("keywords") String keywords,
                                    @RequestParam("authors") String authors) {
        String fileUrl = paperService.uploadPaper(file, title, abstractText, keywords, authors);
        return Result.success(fileUrl, "上传成功");
    }
    
    /**
     * 获取论文信息
     */
    @GetMapping("/info/{id}")
    public Result<Paper> getPaperInfo(@PathVariable Long id) {
        Paper paper = paperService.getPaperById(id);
        return Result.success(paper);
    }
    
    /**
     * 更新论文信息
     */
    @PutMapping("/update")
    public Result<String> updatePaper(@Valid @RequestBody Paper paper) {
        paperService.updatePaper(paper);
        return Result.success("更新成功");
    }
    
    /**
     * 删除论文
     */
    @DeleteMapping("/delete/{id}")
    public Result<String> deletePaper(@PathVariable Long id) {
        paperService.deletePaper(id);
        return Result.success("删除成功");
    }
    
    /**
     * 下载论文
     */
    @GetMapping("/download/{id}")
    public Result<String> downloadPaper(@PathVariable Long id) {
        String downloadUrl = paperService.downloadPaper(id);
        return Result.success(downloadUrl);
    }
}
